aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/[username]/(profile)/+page.server.js
blob: 3110ec4031b094fed87bb2e086ffeb56bd242a5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { auth, get } from "$lib/utils";

export async function load({ cookies, params, parent, url }) {
  let { user, subject } = await parent();
  let { pubkey } = subject;
  let { since = 0 } = params;

  let messages, invoices, sent, received, notes;
  messages = invoices = sent = received = notes = [];

  if (pubkey) {
    if (user) {
      try {
        messages = await get(`/${user.pubkey}/${since}/messages`);
        messages = messages.sort((a, b) => b.created_at - a.created_at);
      } catch (e) {
        console.log(`failed to fetch nostr messages`, e);
      }

      ({ invoices, sent, received } = await get("/requests", auth(cookies)));
    }

    try {
      notes = await get(`/${pubkey}/notes`);
    } catch (e) {
      console.log(`failed to fetch nostr notes for ${pubkey}`, e);
    }

    notes.map((e) => {
      e.seen = e.created_at;
    });
  }

  return { invoices, messages, notes, sent, received };
}